package binaryx

import (
	"encoding/binary"
	"fmt"
	"gitee.com/zhongguo168a/gocodes/myx/errorx"
	"io"
)

func WriteBool(w io.Writer, order binary.ByteOrder, val bool) error {
	return binary.Write(w, order, &val)
}

func WriteInt8(w io.Writer, order binary.ByteOrder, val int8) error {
	return binary.Write(w, order, &val)
}

func WriteInt16(w io.Writer, order binary.ByteOrder, val int16) error {
	return binary.Write(w, order, &val)
}
func WriteInt32(w io.Writer, order binary.ByteOrder, val int32) error {
	return binary.Write(w, order, &val)
}
func WriteInt64(w io.Writer, order binary.ByteOrder, val int64) error {
	return binary.Write(w, order, &val)
}
func WriteUint8(w io.Writer, order binary.ByteOrder, val uint8) error {
	return binary.Write(w, order, &val)
}
func WriteUint16(w io.Writer, order binary.ByteOrder, val uint16) error {
	return binary.Write(w, order, &val)
}
func WriteUint32(w io.Writer, order binary.ByteOrder, val uint32) error {
	return binary.Write(w, order, &val)
}
func WriteUint64(w io.Writer, order binary.ByteOrder, val uint64) error {
	return binary.Write(w, order, &val)
}
func WriteFloat32(w io.Writer, order binary.ByteOrder, val float32) error {
	return binary.Write(w, order, &val)
}
func WriteFloat64(w io.Writer, order binary.ByteOrder, val float64) error {
	return binary.Write(w, order, &val)
}

func WriteBytes(w io.Writer, order binary.ByteOrder, data []byte) (err error) {
	err = binary.Write(w, order, int16(len(data)))
	if err != nil {
		return err
	}
	err = binary.Write(w, order, data)
	if err != nil {
		return err
	}

	return nil
}

func WriteBytesLength(w io.Writer, order binary.ByteOrder, data []byte) (err error) {
	err = binary.Write(w, order, data)
	if err != nil {
		return err
	}

	return nil
}

func WriteUTF(w io.Writer, order binary.ByteOrder, data string) (err error) {
	err = binary.Write(w, order, int16(len(data)))
	if err != nil {
		return
	}
	err = binary.Write(w, order, []byte(data))
	if err != nil {
		return
	}
	return
}

func ReadBool(r io.Reader, order binary.ByteOrder) (data bool, err error) {
	err = binary.Read(r, order, &data)
	return
}

func ReadInt8(r io.Reader, order binary.ByteOrder) (data int8, err error) {
	err = binary.Read(r, order, &data)
	return
}

func ReadInt16(r io.Reader, order binary.ByteOrder) (data int16, err error) {
	err = binary.Read(r, order, &data)
	return
}

func ReadInt32(r io.Reader, order binary.ByteOrder) (data int32, err error) {
	err = binary.Read(r, order, &data)
	return
}

func ReadInt64(r io.Reader, order binary.ByteOrder) (data int64, err error) {
	err = binary.Read(r, order, &data)
	return
}

func ReadUint8(r io.Reader, order binary.ByteOrder) (data uint8, err error) {
	err = binary.Read(r, order, &data)
	return
}

func ReadUint16(r io.Reader, order binary.ByteOrder) (data uint16, err error) {
	err = binary.Read(r, order, &data)
	return
}

func ReadUint32(r io.Reader, order binary.ByteOrder) (data uint32, err error) {
	err = binary.Read(r, order, &data)
	return
}

func ReadUint64(r io.Reader, order binary.ByteOrder) (data uint64, err error) {
	err = binary.Read(r, order, &data)
	return
}

func ReadFloat32(r io.Reader, order binary.ByteOrder) (data float32, err error) {
	err = binary.Read(r, order, &data)
	return
}

func ReadFloat64(r io.Reader, order binary.ByteOrder) (data float64, err error) {
	err = binary.Read(r, order, &data)
	return
}

func ReadBytes(r io.Reader, order binary.ByteOrder) (data []byte, err error) {
	var tlen int16
	err = binary.Read(r, order, &tlen)
	if err != nil {
		err = errorx.Wrap(err, fmt.Sprintf("read length"))
		return
	}
	data = make([]byte, tlen)
	err = binary.Read(r, order, &data)
	if err != nil {
		err = errorx.Wrap(err, fmt.Sprintf("read body"))
		return
	}
	return
}

func ReadBytesLength(r io.Reader, order binary.ByteOrder, length int16) (data []byte, err error) {
	data = make([]byte, length)
	err = binary.Read(r, order, &data)
	if err != nil {
		err = errorx.Wrap(err, fmt.Sprintf("read body"))
		return
	}
	return
}

func ReadUTF(r io.Reader, order binary.ByteOrder) (data string, err error) {
	tlen := int16(0)
	err = binary.Read(r, order, &tlen)
	if err != nil {
		err = errorx.Wrap(err, fmt.Sprintf("read length"))
		return
	}
	tbs := make([]byte, tlen)
	err = binary.Read(r, order, &tbs)
	if err != nil {
		err = errorx.Wrap(err, fmt.Sprintf("read body"))
		return
	}
	data = string(tbs)
	return
}
